package com.hnhegui.backend.platform.service.payment;

import com.hnhegui.backend.entity.Result;
import com.hnhegui.backend.order.api.OrderRestApi;
import com.hnhegui.backend.order.dto.response.PaymentOrderCallbackResponseDTO;
import com.hnhegui.backend.payment.api.PaymentRestApi;
import com.hnhegui.backend.payment.dto.response.PaymentResponseDTO;
import com.hnhegui.backend.platform.convert.payment.PaymentConvert;
import com.hnhegui.backend.platform.vo.payment.request.PaymentCallbackRequestVO;
import com.hnhegui.backend.platform.vo.payment.request.PaymentRequestVO;
import com.hnhegui.backend.platform.vo.payment.response.PaymentCallbackResponseVO;
import com.hnhegui.backend.platform.vo.payment.response.PaymentResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 刘大海
 * @since 2025/10/27 11:28
 */
@Service
public class RestPaymentService {

    @Autowired
    PaymentRestApi paymentRestApi;

    @Autowired
    OrderRestApi orderRestApi;

    public PaymentResponseVO initiatePayment(PaymentRequestVO request) {
        Result<PaymentResponseDTO> result = paymentRestApi.pay(PaymentConvert.INSTANCE.convertRequest(request));
        return PaymentConvert.INSTANCE.convertResponse(result.getData());
    }

    public PaymentCallbackResponseVO handlePaymentCallback(PaymentCallbackRequestVO request) {
        Result<PaymentOrderCallbackResponseDTO> paymentCallbackResponseDTOResult =
            orderRestApi.paymentCallback(PaymentConvert.INSTANCE.convertCallBackRequest(request));
        return PaymentConvert.INSTANCE.convertCallBackResponse(paymentCallbackResponseDTOResult.getData());
    }
}
